package com.uk.ui.kontrata;

import java.util.Arrays;
import java.util.List;

import com.uk.HandleMessages;
import com.uk.data.containers.KontrataContainer;
import com.uk.data.entities.BashkiKomuna;
import com.uk.data.entities.Kontrata;
import com.uk.data.entities.Perdorimi;
import com.uk.data.entities.Qarku;
import com.uk.data.entities.Rrethi;
import com.uk.exceptions.NestedException;
import com.uk.test.Person;
import com.vaadin.data.Item;
import com.vaadin.data.Property;

import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class KontrataForm extends Form implements ClickListener{

	private Button save = new Button("Ruaj", (ClickListener) this);
	private Button cancel = new Button("Anullo", (ClickListener) this);
	private Button edit = new Button("Ndrysho", (ClickListener) this);
	
	private final Select perdorimiSelect;
	private final Select qarkuSelect;
	private final Select rrethiSelect;
	private final Select bashkiKomunaSelect;
	
	
	private KontrataPanel kontrataPanel;
	private boolean addKontrataMode = false;
	private Kontrata newKontrate = null;
	
	
	
	public KontrataForm(KontrataPanel kontrataPanel){
		this.kontrataPanel = kontrataPanel;
		this.perdorimiSelect  = new Select("Perdorimi",kontrataPanel.getPerdorimiContainer());
		this.qarkuSelect = new Select("Qarku", kontrataPanel.getQarkuContainer());
		this.qarkuSelect.addListener(this.getValueChangeListener());
		this.qarkuSelect.setImmediate(true);
		
		this.rrethiSelect = new Select("Rrethi");
		this.rrethiSelect.addListener(this.getValueChangeListener());
		this.rrethiSelect.setImmediate(true);
		
		this.bashkiKomunaSelect = new Select("Bashki/Komuna");
		this.bashkiKomunaSelect.setImmediate(true);
		setWriteThrough(false);
		this.setInvalidCommitted(false);
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSpacing(true);
		footer.addComponent(save);
		footer.addComponent(cancel);
		footer.addComponent(edit);
		footer.setVisible(false);

		setFooter(footer);
		
	
		setFormFieldFactory(new DefaultFieldFactory(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 3757916439045659838L;

			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				
				Field field = super.createField(item, propertyId, uiContext);
				if (propertyId.equals("perdorimi")){
					perdorimiSelect.setRequired(true);
					perdorimiSelect.setRequiredError("Perdorimi nuk duhet te jete bosh!");
					perdorimiSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
					perdorimiSelect.setItemCaptionPropertyId("tag");
					perdorimiSelect.setWidth("200px");
					return perdorimiSelect;
				}
				
				
				
				if (propertyId.equals("bashkiKomuna")){
					
					
					qarkuSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
					qarkuSelect.setItemCaptionPropertyId("tag");
					qarkuSelect.setWidth("200px");
					attachField("qarku", qarkuSelect);
					
					rrethiSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
					rrethiSelect.setItemCaptionPropertyId("tag");
					rrethiSelect.setWidth("200px");
					attachField("rrethi", rrethiSelect);
					
					bashkiKomunaSelect.setRequired(true);
					bashkiKomunaSelect.setRequiredError("Bashki/Komuna nuk duhet te jete bosh!");
					bashkiKomunaSelect.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
					bashkiKomunaSelect.setItemCaptionPropertyId("tag");
					bashkiKomunaSelect.setWidth("200px");
					return bashkiKomunaSelect;
				}

				if (propertyId.equals("emri")){
					 TextField tf = (TextField) field;
					 tf.setMaxLength(30);
		             tf.setRequired(true);
		             tf.setRequiredError("Emri nuk duhet te jete bosh!");
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if (propertyId.equals("mbiemri")){
					 TextField tf = (TextField) field;
					 tf.setMaxLength(30);
		             tf.setRequired(true);
		             tf.setRequiredError("Mbiemri nuk duhet te jete bosh!");
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				if (propertyId.equals("adresa")){
					 TextField tf = (TextField) field;
		             tf.setMaxLength(255);
		             tf.setWidth("200px");
		             tf.setNullRepresentation("");
		             return tf;
				}
				
				
				return null;
				
				
			}
			
			
			
			
		});
		
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		Button source = event.getButton();
		
		try {
			if (source == save){
				//if (!isValid()) return;
				try {
					commit();
					newKontrate = kontrataPanel.getFaturaBean().saveKontrata(((BeanItem<Kontrata>)this.getItemDataSource()).getBean());
					
					if(addKontrataMode){
						Item addedItem = kontrataPanel.getDataSource().addItem(newKontrate);
						setItemDataSource(addedItem);
						addKontrataMode = false;
					}
					setReadOnly(true);
					HandleMessages.showInformationOnScree(HandleMessages.SUCCESS_SAVE, this);
				} catch (NestedException e) {
					HandleMessages.showErrorOnScreen(e, this);
				
				} catch (Exception e) {
					HandleMessages.showErrorOnScreen(e, this);
				}
				
				
			}
			else if (source == cancel){
				if(addKontrataMode){
					addKontrataMode = false;
					setItemDataSource(null);
				}else {
					discard();
				}
				setReadOnly(true);
			}
			else if (source == edit) {
				setReadOnly(false);
			}
		} catch (Exception e) {
			HandleMessages.showErrorOnScreen(e, this);
		}
		
	}

	@Override
	public void setItemDataSource(Item newDataSource) {
		addKontrataMode = false;
		this.setVisible(true);
		if(newDataSource != null){
			setReadOnly(false);
			List<Object> orderedProperties = Arrays.asList(KontrataContainer.NATURAL_COL_ORDER);
			super.setItemDataSource(newDataSource, orderedProperties);
			this.perdorimiSelect.setValue(((BeanItem<Kontrata>)newDataSource).getBean().getPerdorimi());
			BashkiKomuna bk = ((BeanItem<Kontrata>)newDataSource).getBean().getBashkiKomuna();
			
			if (bk != null){
				this.qarkuSelect.setValue(bk.getRrethi().getQarku());
				this.qarkuSelect.addListener(this.getValueChangeListener());
				if (bk.getRrethi() != null ){
					this.rrethiSelect.setValue(bk.getRrethi());
				}
			}
				
			
			this.bashkiKomunaSelect.setValue(bk);
			setReadOnly(true);
			getFooter().setVisible(true);
		}
		else {
			super.setItemDataSource(null);
			this.setVisible(false);
			getFooter().setVisible(false);
		}
		
	}
	
	
	@Override
	public void setReadOnly(boolean readOnly) {
		super.setReadOnly(readOnly);
		save.setVisible(!readOnly);
		cancel.setVisible(!readOnly);
		edit.setVisible(readOnly);
		qarkuSelect.setReadOnly(readOnly);
		rrethiSelect.setReadOnly(readOnly);
		bashkiKomunaSelect.setReadOnly(readOnly);
		
		
	}
	
	public void addKontrate(){
		newKontrate = new Kontrata();
		
		setItemDataSource(new BeanItem(newKontrate));
		addKontrataMode = true;
		setReadOnly(false);
		this.qarkuSelect.setValue(null);
		this.rrethiSelect.setValue(null);
	}
	
	
	public Property.ValueChangeListener getValueChangeListener(){
		return new Property.ValueChangeListener() {

			@Override
			public void valueChange(
					com.vaadin.data.Property.ValueChangeEvent event) {
				Property property = event.getProperty();
				if (property == qarkuSelect){
					rrethiSelect.setValue(null);
					BeanItemContainer<Rrethi> c = kontrataPanel.getRrethiContainer((Qarku) qarkuSelect.getValue());
					if (c == null)rrethiSelect.removeAllItems();
					else rrethiSelect.setContainerDataSource(c);	
				}
				else if (property == rrethiSelect){
					bashkiKomunaSelect.setValue(null);
					BeanItemContainer<BashkiKomuna> c = kontrataPanel.getBashkiKomunaContainer((Rrethi) rrethiSelect.getValue());
					if (c == null) bashkiKomunaSelect.removeAllItems();
					else bashkiKomunaSelect.setContainerDataSource(c);
						
				}
				
				
			}
		
		};
	}

}
